home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 1
/
Cream of the Crop 1.iso
/
PROGRAM
/
SWAPUNIT.ARJ
/
SWAPUNIT.DOC
< prev
next >
Wrap
Text File
|
1992-01-20
|
17KB
|
357 lines
SWAPUNIT: Turbo Pascal Unit
Copyright 1992 Kevin Kwast
January 20, 1992
SWAPUNIT allows you to execute a program (like the Turbo
Pascal Exec procedure) with the maximum of available memory. Just
add SWAPUNIT to Uses clause and use SwapExec instead of the Turbo
Pascal Exec. Memory previously used by your program will be
swapped to XMS, EMS, or the hard disk, freeing up more available
memory for the executed program or DOS shell.
SWAPUNIT requires Turbo Pascal version 4 or above. Units
are included which were compiled under versions 4.0, 5.5, and 6.0
for your convenience. They are named SWAPUNIT.TP4 and so on.
Rename your choice to SWAPUNIT.TPU and use it.
The results can be astounding, particularly with programs
that use a lot of stack or heap space. The SwapExec procedure
is hand-coded in assembly language for optimal speed and minimum
overhead. A child program or DOS shell can be spawned with as
little as 3K overhead. Sound unbelievable? Try it! You have 30
days to examine SWAPUNIT and see if it meets your needs; if you
decide to use SWAPUNIT, please register your usage with the
author. See the end of this documentation for information.
Here is the interface section of SWAPUNIT, which includes
comments describing the unit's usage and results:
Unit SwapUnit; { Kevin A. Kwast }
(**********************************************************************)
(** This unit provides Turbo Pascal programs with an ability to swap **)
(** its memory blocks to XMS, EMS, or the hard disk (in that order) **)
(** and EXEC another program. Put this unit last in the Uses list **)
(** for best performance (Turbo Pascal links in reverse). The Try **)
(** parameter allows the caller to specify which methods of swapping **)
(** will be attempted. They are additive, so SwapToEMS+SwapToXMS **)
(** will try swapping to EMS and XMS, but not to the disk. The word **)
(** result of the SwapExec function is the result code, with the Hi **)
(** and Lo bytes having the following meanings: **)
(** **)
(** Hi(Result) = 0: Success, the swapping and execution was OK. **)
(** The DOS errorlevel result is in Lo(Result). **)
(** = 1: An error in the DOS memory chain prevented **)
(** swapping. This is unlikely. **)
(** = 2: Unable to swap the program to any of the **)
(** methods allowed. **)
(** = 3: Swapping was alright, but the program name **)
(** specified could not be executed. The low **)
(** byte describes the execution problem. **)
(** **)
(** Lo(Result) = 2: Program name could not be found to execute. **)
(** = 5: Couldn't open the program name. **)
(** = 8: Insufficient memory to run the program. **)
(** **)
(**********************************************************************)
Interface
Const
SwapToDisk = 4;
SwapToEMS = 2;
SwapToXMS = 1;
SwapToAny = 7;
Function SwapExec(Program_Name, Command_Line, Swap_FName: String;
Try: Byte) : Word;
Function CheckEMS : Boolean;
Function CheckXMS : Boolean;
-------------------
Function SwapExec
-------------------
Parameter: Program_Name This is a Pascal string of any type
containing the program which will be
executed. This string is a DOS file
specification which MUST include a
file name and extension, and may
include a drive and path. Any drive
accessible by DOS is valid here.
Parameter: Command_Line This command line will be passed to
the executed program.
Parameter: Swap_FName This is a DOS file specification to
be used if it is necessary to swap
to disk. This should be specified
if SwapExec will try to swap to disk.
Parameter: Try This byte describes which storage
forms will be attempted in swapping.
The constants SwapToXMS, SwapToEMS,
SwapToDisk, and SwapToAny have been
provided for user convenience.
Result The word returned by the function
describes the results of the swap
and program execution. Check the
high and low bytes for individual
status codes documented above.
-------------------
Function CheckEMS
-------------------
Result The boolean result returns TRUE if
LIM EMS 4.0 or compatible expanded
memory is available. This is the
form of expanded memory required
in order to swap to EMS.
-------------------
Function CheckXMS
-------------------
Result The boolean result returns TRUE if
a Microsoft XMS or compatible driver
provides extended memory support.
A driver like this is necessary to
provide XMS support to SWAPUNIT.
In order to get the optimum performance from SWAPUNIT, you
need to understand how SWAPUNIT works, how Turbo Pascal allocates
memory, and how you can use Turbo Pascal's memory allocation
characteristics to your advantage.
First of all, program memory is allocated as one large block
including the program code, unit code, data, stack, and heap space.
SWAPUNIT can save everything after itself in the memory block to
XMS, EMS, or the hard disk. SwapExec will try XMS, then EMS, and
then try to save to disk (you can allow swapping to only some of
those forms with the Try parameter). If swapping is successful,
the memory block is reduced in size and the child program is
executed. After the child program returns, SWAPUNIT (which was
not swapped out) will reload the rest of the memory block and
restore the DOS memory block to its former status. Graphically,
an example program being swapped looks like this:
Before calling SwapExec: After SWAPUNIT Swaps:
------------------- -------------------
/ 40K Free Memory \ / \
|=================| / All Of This Is \
| 500K Heap | / Swapped Out \
|=================| / And Available! \
| 16K Stack | / \
|=================| / 574K Free \
| 10K Data | / Memory! \
|=================| |=================|
| 10K Units Code | | 2K SWAPUNIT |
|=================| |=================|
| 5K Program Code | | 5K Program Code |
------------------- -------------------
Since SWAPUNIT can only swap out what appears after itself
in memory, you want SWAPUNIT to be the first unit in memory, and
you want the program code (all the code in the main Pascal file)
segment to be as small as possible. For those using Turbo Pascal
version 5 or above, compile your program with map file generation
(this is the /GS parameter in TPC, the command line compiler).
Take a look at the .MAP file to see how memory will be used by
the compiled program. You will see your program's name appearing
as the first area in the .MAP file, with areas appearing later
(or higher) in memory further on in the MAP file. Note that the
SYSTEM unit (which Turbo Pascal always includes) is automatically
linked as the last unit in memory; this is not a concern.
SwapExec will swap out everything after the SWAPUNIT area
and restore it when SwapExec returns. Turbo Pascal will always
put the data, stack, and heap where they can be swapped out, but
maximizing the unit and program code space which can be swapped
out requires a little inside knowledge.
Thus, your goal is to have a small main program area with
SWAPUNIT immediately after that. Here are a few tips and facts
to make this feat easier to accomplish:
* String constants will be in the code segment. This means that
if your main program contains a great deal of literal strings
[such as things like: WriteLn('Hello there.');], your code
segment can get very large. The best solution to this problem
is declaring your strings in the Const section (which are
allocated in the data segment) and then use the constant.
[For example: Const Str1 = 'Hello there.'; WriteLn(Str1);]
You will see this technique in the SWAPTEST example program.
* Each unit occupies its own segment. These are linked in the
reverse order of their appearance on the Uses line, so that
the last unit on the line will be the earliest in memory.
* The program's memory block will be loaded in order (from first
to last in memory): the main program code, the unit segments
(where each unit has its own code and data), the main program
data, the stack, and the heap.
* In order to keep the main program code as small as possible
(since this is unavoidable overhead), put as much as you can
in seperate units. A trick which I use goes like this: Put
the basis of the program in a unit, which is then included
in a "shell" whose sole purpose is to call the program in its
unit. In this way, you can link SWAPUNIT before the large
unit and have large applications use SwapExec with about 3
kilobytes of memory overhead. In order to allow the large
unit to call SwapExec (which is in the SWAPUNIT), you will
need to have a symbiotic dependency (so that the large unit
containing the bulk of the program uses SwapUnit and SwapUnit
uses the large unit). In order to do this trick, you will
need the SWAPUNIT source code, which is given to registered
users. See the end of the documentation for details.
See the enclosed SWAPTEST example Turbo Pascal program and the
commented SWAPTEST.MAP file (produced by Turbo Pascal 6.0) for
an example of SwapExec usage and results.
Please note that the SwapExec "swap to disk" function requires
the user to have at least DOS 3.0, while everything else needs
DOS 2.0 (as does anything, really). If the user doesn't have
DOS 3.0 or above, swapping to disk will automatically fail.
-----------------------------------------------------------------
D I S C L A I M E R
-----------------------------------------------------------------
Every effort has been made to insure that SWAPUNIT works as
described, and runs completely error-free. No guarantee is
made as to the suitability of SWAPUNIT to any purpose, and I
cannot be responsible for any damages alledged to have been
caused by the use of SWAPUNIT. If you find a problem with
the usage of SWAPUNIT, please alert the author at the address
below. SWAPUNIT will continue to go through testing and
modification to ensure the best possible product.
-----------------------------------------------------------------
A B O U T T H I S P R O G R A M
-----------------------------------------------------------------
SWAPUNIT is Copyright 1992 by Kevin A. Kwast, and protected
by copyright laws under U.S. and International law. The user
is granted the right to copy and distribute this package in
its complete, unaltered form. Usage of SWAPUNIT requires a
Shareware registration fee be paid to the author. In this
way, you can examine SWAPUNIT and see if it meets your needs
before you purchase the program. Take it for a spin; drive
it around the block a few times. Try it out for 30 days, but
please note that you may not use beyond this time (or use it
in any program) until you register your usage. When you do,
you will receive the Turbo Pascal source to SWAPUNIT (but not
the assembly language source to the SwapExec routine), so you
can remove the registration notice from the start of the unit
and create a more specialized unit which will eliminate
overhead (through each unit using the other, as I mentioned
above). Specify disk type when you register, and you will
receive the source code and additional examples so that you
can begin producing the best programs possible.
Thank you in advance for your support of Shareware products.
Months of effort were put into this product, and your support
shows that you appreciate products like these. Is $35 worth
the effort you would put into designing something like this
yourself? You can use the form on the next page (which is
also found in the file REGISTER.ME) -- Thank you again!
-----------------------------------------------------------------
R E G I S T R A T I O N F O R M
-----------------------------------------------------------------
Print and complete this form to send with check or money order
in the amount of $35 (US dollars only) payable to KEVIN KWAST.
Mail to the address below, and thank you for your support; your
registration makes quality Shareware possible.
Any of this information is optional.
Name: ____________________________________________________
Company: _________________________________________________
Address: _________________________________________________
City: ____________________ State: _______ Zip: _________
Area Code: _________ Phone: _____________________________
Computer Type: ___________________________________________
Turbo Pascal Version: ___________________ Modem? ________
Other Languages You Use: _________________________________
Disk Size Preferred: [___] 5.25 inch [____] 3.5 inch
Where Did You Get SWAPUNIT: ______________________________
What Do You Think: _______________________________________
What Will It Be Used For: ________________________________
You can contact me for registration or support:
Kevin A. Kwast
P. O. Box 1397
Coppell, Texas
75019